package cn.jksoft.businessclient.ui.activity;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import cn.jksoft.businessclient.R;
import cn.jksoft.businessclient.base.BaseActivity;
import cn.jksoft.businessclient.base.BaseApplication;
import cn.jksoft.businessclient.widgets.CustomWebView;
import cn.jksoft.businessclient.widgets.MySwipeRefreshLayout;

public class WebViewActivity extends BaseActivity {

    private CustomWebView webView;
    private int storeId,customerId,companyId,tenantId;
    private String username;
    private String url = "http://120.25.224.27:21080/chekoo/store/index.html";
    private long exitTime = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);
        initTitle("51车宝商务端");
        toolbar.setNavigationIcon(null);

        username = BaseApplication.getInstance().getLoginEntity().getPuxsoftLoginDTO().getAdminUserDTO().getLogin();
        storeId = BaseApplication.getInstance().getLoginEntity().getPuxsoftLoginDTO().getStoreId();
        customerId = BaseApplication.getInstance().getLoginEntity().getPuxsoftLoginDTO().getAdminUserId();
        companyId = BaseApplication.getInstance().getLoginEntity().getPuxsoftLoginDTO().getCompanyId();
        tenantId = BaseApplication.getInstance().getLoginEntity().getPuxsoftLoginDTO().getTenantId();

        initView();

    }

    @Override
    protected void onDestroy() {
        this.webView.destroy();
        super.onDestroy();
    }

    private void initView() {
        webView = (CustomWebView) findViewById(R.id.webview);
        MySwipeRefreshLayout swipeLayout = (MySwipeRefreshLayout) findViewById(R.id.swipeLayout);

        webView.setSwipeLayout(swipeLayout);
        swipeLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                webView.loadUrl(webView.getUrl());
            }
        });

        swipeLayout.setColorSchemeColors(
                ContextCompat.getColor(getBaseContext(), R.color.main_red),
                ContextCompat.getColor(getBaseContext(), R.color.main_red2),
                ContextCompat.getColor(getBaseContext(), R.color.green),
                ContextCompat.getColor(getBaseContext(), R.color.gray));

        initTitle("51车宝商务端");

        webView.setWebViewClient(new WebViewClient() {

            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                if (url.startsWith("tel:") || url.startsWith("mailto:") ) {

                }else {
                    view.loadUrl(url);
                }
                return true;
            }
        });

        if (url.startsWith("http://") || url.startsWith("https://")) {
            webView.loadUrl(url+"?userName="+username+"&customerId="+customerId+"&companyId="+companyId+"&tenantId="+tenantId);
        } else {
            webView.loadUrl("http://" + url);
        }
    }

    @Override
    // 设置回退
    // 覆盖Activity类的onKeyDown(int keyCoder,KeyEvent event)方法
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
            webView.goBack(); // goBack()表示返回WebView的上一页面

            return true;
        }else {
            exit();
            return false;
        }
    }

    public void exit() {
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            Toast.makeText(getApplicationContext(), "再按一次退出程序",
                    Toast.LENGTH_SHORT).show();
            exitTime = System.currentTimeMillis();
        } else {
            System.exit(0);
        }
    }
}
